package cmd

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/net/goai"
	"github.com/gogf/gf/v2/os/gcmd"
	"qiyu-contact/internal/controller/common"
	"qiyu-contact/internal/controller/friend"
	"qiyu-contact/internal/controller/user"
	"qiyu-contact/internal/packed/socket"
	"qiyu-contact/internal/service"
	"strings"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			api := s.GetOpenApi()
			api.Info.Title = "奇遇后台管理系统"
			api.Info.Description = "奇遇后台管理系统"
			api.Info.Version = "v1"
			api.Info.Contact = &goai.Contact{
				Name:  "fish",
				Email: "1915144429@qq.com",
			}
			socket.StartWebsocket(ctx)
			s.BindHandler("/ws/:id", socket.RegisterWebsocket)

			s.Group("/api", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareCORS, ghttp.MiddlewareHandlerResponse, func(r *ghttp.Request) {
					if strings.Contains(r.URL.Path, "ws") || strings.Contains(r.URL.Path, "user/login") || strings.Contains(r.URL.Path, "user/register") || strings.Contains(r.URL.Path, "user/findUserAccount") {
						r.Middleware.Next()
					} else {
						service.Auth().MiddlewareFunc()(r)
						r.Middleware.Next()
					}
				})
				group.Group("/v1", func(v1Group *ghttp.RouterGroup) {
					v1Group.Bind(user.New(), common.New(), friend.New())
				})

			})
			s.Run()
			return nil
		},
	}
)
